#coding=utf-8
import sys
import os
from   os.path import abspath, dirname
sys.path.insert(0,abspath(dirname(__file__)))
import tkinter
from   tkinter import *
import Fun
import threading
import time

uiName="console"
ElementBGArray={}
ElementBGArray_Resize={}
ElementBGArray_IM={}

global pause

#Form 'Form_1's Event :Load
def Form_1_onLoad(uiName): 
    pass
#Button 'Button_1' 's Command Event :
def Button_1_onCommand(uiName,widgetName):
    from Project1_cmd import mqtt_client
    publish_topic=Fun.GetText(uiName,'ComboBox_1')
    publish_msg=Fun.GetText(uiName,'Text_1')
    mqtt_client.publish(publish_topic,publish_msg) 
    Fun.AddItemText(uiName,'ListBox_1','发送主题：%s'%publish_topic,"end")
    Fun.AddItemText(uiName,'ListBox_1',publish_msg,"end")
    pass
#Button 'Button_2' 's Command Event :

def show_submsg():
    from Project1_cmd import mqtt_client
    global pause
    mqtt_client.subscribe('/devices/#')
    message = mqtt_client.get_message() 
    if message and pause:
        topic = message[0]
        payload = message[1]
            # Process the received message
        print(f"Processing message from topic '{topic}': '{payload}'")
        Fun.AddItemText(uiName,'ListBox_1',"订阅主题：%s"%topic,"end")
        Fun.AddItemText(uiName,'ListBox_1',payload,"end")
    timer = threading.Timer(1,show_submsg)
    timer.start()

def Button_2_onCommand(uiName,widgetName):  
    global pause
    pause=True
    show_submsg()
    pass
#Button 'Button_3' 's Command Event :
def Button_3_onCommand(uiName,widgetName):
    global pause
    pause=False
    pass
#ComboBox 'ComboBox_1's ComboboxSelected Event :
def ComboBox_1_onSelect(event,uiName,widgetName):
    topic_name=Fun.GetText(uiName,'ComboBox_1')
    if topic_name=="/devices/{deviceid}/login/up":
        
    pass
